#! /bin/bash
echo "It will install lamp or lnmp."
sleep 1

## check last command is OK or not.
function check_ok() {
    if [ $? -ne 0 ]; then
        echo "Error, Check the error log."
        exit 1
    fi
}

## Get archive of the system, i686 or x86_64
ar=`arch`

## close selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

selinux_status=`getenforce`
if [ $selinux_status == "Enforcing" -o $selinux_status == "enforcing" ]; then
    setenforce 0
fi

## close iptables
iptables-save > /etc/sysconfig/iptables_`date +%s`
ip6tables-save > /etc/sysconfig/ip6tables_`date +%s`
iptables -F
ip6tables -F
/etc/init.d/iptables save
/etc/init.d/ip6tables save

## if packages installed, then omit.
function myum() {
    if ! rpm -qa | grep -iq "^$1"; then
        yum install -y $1
        check_ok
    else
        echo "$1 is already installed."
    fi
}

## install some packages
for i in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel
do
    myum $i
done

## install epel
if rpm -qa | grep -iq epel-release; then
    rpm -e epel-release
fi

if ls /etc/yum.repos.d/epel* > /dev/null 2>&1; then
    rm -f /etc/yum.repos.d/epel*
fi

wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo

## check user is exist or not
## if the user is exist, then do nothing, else create the specified user.
function check_user () {
    if ! grep "^$1:" /etc/passwd; then
        useradd -M -s /sbin/nologin $1
    fi
}

## check dst install dir, if exist then backup it with timestamp
function check_dst_dir () {
    if [ -d $1 ]; then
        /bin/mv $1 $1_`date +%s`
    fi
}

function kill_process () {
	pid=`ps aux | grep -i $1 | grep -v grep | awk -F ' ' '{print $2}'` 
	for i in $pid; do
		echo $i
		kill -9 $i
	done
}

## check directory is clean, and install the mysqld the specified directory;
## then copy the configuration file to specfy destination, 
## after this generate the start up script int /etc/init.d and used checkconfig --add or --del to the start up list.
function mysql_install_and_generate_the_cfg_ () {
    # use function check_user to check the user mysql is exist or not.
    check_user mysql
    myum compat-libstdc++-33

    check_dst_dir /data/mysql
    /bin/mkdir -p /data/mysql
    chown -R mysql:mysql /data/mysql

    check_dst_dir /usr/local/mysql
    /bin/mv  $1 /usr/local/mysql
    cd /usr/local/mysql
    [ -f /etc/my.cnf ] && /bin/mv /etc/my.cnf /etc/my.cnf_bak_`date +%s`
    ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
    check_ok

    # copy the example config file to with /etc/my.cnf
    if [ -f support-files/my-large.cnf ]; then
        /bin/cp support-files/my-large.cnf /etc/my.cnf
    elif [ -f support-files/my-default.cnf ]; then
        /bin/cp support-files/my-default.cnf /etc/my.cnf
    fi
    check_ok
    sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf

    /bin/cp support-files/mysql.server /etc/init.d/mysqld
    sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
    chmod 755 /etc/init.d/mysqld
    chkconfig --add mysqld
    chkconfig mysqld on
    kill_process mysql
    service mysqld start
    check_ok
}

## function of installing mysqld.
function install_mysqld () {
    echo "chose the mysql version of you wanna."
    select mysql_ver in 5.1 5.6; do
        case $mysql_ver in
            5.1)
                cd /usr/local/src/
                [ -f mysql-5.1.72-linux-$ar-glibc23.tar.gz ] || wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.72-linux-$ar-glibc23.tar.gz
                tar zxf mysql-5.1.72-linux-$ar-glibc23.tar.gz
                check_ok

                mysql_install_and_generate_the_cfg_ mysql-5.1.72-linux-$ar-glibc23
                check_ok

                break
                ;;
            5.6)
                cd /usr/local/src
                [ -f mysql-5.6.29-linux-glibc2.5-$ar.tar.gz ] || wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.29-linux-glibc2.5-$ar.tar.gz
                tar zxf mysql-5.6.29-linux-glibc2.5-$ar.tar.gz
                check_ok
                
                mysql_install_and_generate_the_cfg_ mysql-5.6.29-linux-glibc2.5-$ar
                check_ok

                break
                ;;
            *)
                echo "you can chose '1' or '2' only."
                exit 1
                ;;
        esac
    done
}

## function of install httpd.
function install_httpd () {
    echo "Install apache httpd version 2.2."
    cd /usr/local/src/
    [ -f httpd-2.2.31.tar.bz2 ] || wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.31.tar.bz2
    tar jxf httpd-2.2.31.tar.bz2
    check_ok
    cd httpd-2.2.31
    ./configure \
        --prefix=/usr/local/apache2 \
        --with-included-apr \
        --enable-so \
        --enable-deflate=shared \
        --enable-expires=shared \
        --enable-rewrite=shared \
        --with-pcre
    check_ok
    make
    check_ok
    make install
    check_ok
}

install_httpd
